package com.my.vote.pst.info;

import com.my.vote.VoteApplication;
import com.my.vote.entity.EUser;
import com.my.vote.entity.EVoteInfo;
import com.my.vote.entity.EVoteItem;
import com.my.vote.entity.req.ReqVoteList;
import com.sfh.lib.AppCacheManager;
import com.sfh.lib.mvp.service.AbstractPresenter;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * 功能描述:
 *
 * @author
 * @date 2018/4/18
 */
public class PstMyVoteList extends AbstractPresenter<IPstMyVoteList.IVVoteList> implements IPstMyVoteList {
    @Override
    public void listPage(final ReqVoteList req) {
        BmobQuery<EVoteInfo> query = new BmobQuery<>();
        final EUser user = AppCacheManager.newInstance().getCache(VoteApplication.USER, new EUser());
        query.addWhereEqualTo("userId", user.getObjectId());
        queryVoteInfo(query, req.getNowPage());
    }

    @Override
    public void listSigUpPage(final ReqVoteList req) {
        BmobQuery<EVoteItem> queryItem = new BmobQuery<>();
        final EUser user = AppCacheManager.newInstance().getCache(VoteApplication.USER, new EUser());
        queryItem.addWhereEqualTo("createUserId", user.getObjectId());
        // 限制最多10条数据结果作为一页
        queryItem.setLimit(10);
        // 忽略前10条数据（即第一页数据结果）
        queryItem.setSkip(10 * req.getNowPage());
        // 根据score字段降序显示数据
        queryItem.order("-createdAt");
        queryItem.findObjects(AppCacheManager.getApplication(), new FindListener<EVoteItem>() {
            @Override
            public void onSuccess(List<EVoteItem> list) {
                if (!list.isEmpty()) {
                    List<String> voteID = new ArrayList<>(list.size());

                    for (EVoteItem item : list) {
                        voteID.add(item.getVoteId());
                    }
                    BmobQuery<EVoteInfo> query = new BmobQuery<>();
                    query.addWhereContainsAll("objectId", voteID);
                    queryVoteInfo(query, req.getNowPage());
                } else {
                    getView().onPageError("No Data!");
                }
            }

            @Override
            public void onError(int i, String s) {
                getView().onPageError("No Data!");
            }
        });


    }

    private void queryVoteInfo(BmobQuery<EVoteInfo> query, final int nowPage) {
        // 限制最多10条数据结果作为一页
        query.setLimit(10);
        // 忽略前10条数据（即第一页数据结果）
        query.setSkip(10 * nowPage);
        // 根据score字段降序显示数据
        query.order("-createdAt");
        query.findObjects(AppCacheManager.getApplication(), new FindListener<EVoteInfo>() {
            @Override
            public void onSuccess(List<EVoteInfo> list) {
                if (!list.isEmpty()) {

                    getView().onPageList(nowPage, list.size() >= 10 ? nowPage + 1 : nowPage, list);
                    addVisitSiz(list);

                } else {
                    getView().onPageError("No Data!");
                }
            }

            @Override
            public void onError(int i, String s) {
                getView().onPageError("No Data!");
            }
        });
    }

    private void addVisitSiz(List<EVoteInfo> list) {
        List<BmobObject> objects = new ArrayList<>(list.size());
        for (EVoteInfo info : list) {
            int visitSize = info.getVisitSize() + 1;
            info.setVisitSize(visitSize);
            objects.add(info);
        }
        new BmobObject().updateBatch(AppCacheManager.getApplication(), objects, new UpdateListener() {
            @Override
            public void onSuccess() {

            }

            @Override
            public void onFailure(int i, String s) {

            }
        });
    }
}
